package com.woniuxy.cloud.starter.context.openfeign;

import com.woniuxy.cloud.starter.context.AppContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;

/**
 * 用于将基于feign的远程服务调用中传递请求上下文信息
 */
public class ContextInjectionFeignInterceptor implements RequestInterceptor {

    protected void addHeader(RequestTemplate requestTemplate, String name,
                             String... values) {

        if (!requestTemplate.headers().containsKey(name)) {
            requestTemplate.header(name, values);
        }
    }

    @Override
    public void apply(RequestTemplate template) {
        if(AppContext.initialized()) {
            addHeader(template, "operatorId", ""+AppContext.getOperatorId());
            addHeader(template, "operatorName", AppContext.getOperatorName());
        }
    }
}
